import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
// import Home from "../views/Home.vue";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/restback",
  },
  {
    path: "/option",
    component: () => import("../views/option/Index.vue"),
  },
  {
    path: "/login",
    component: () => import("../views/login/Index.vue"),
  },
  {
    path: "/restback",
    name: "Home",
    redirect: "/restback/index",
    component: () => import("../views/home/Index.vue"),
    children: [
      {
        path: "/restback/index",
        component: () => import("../views/home/RBmain.vue"),
      },
      {
        path: "/restback/menu",
        component: () => import("../views/menu/Index.vue"),
      },
      {
        path: "/restback/order",
        component: () => import("../views/order/Index.vue"),
      },
      {
        path: "/restback/search",
        component: () => import("../views/search/Index.vue"),
      },
      {
        path: "/restback/worker",
        component: () => import("../views/worker/Index.vue"),
      },
      {
        path: "/restback/inventory",
        component: () => import("../views/inventory/Index.vue"),
      },
    ],
  },
  // {
  //   path: "/about",
  //   name: "About",
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ "../views/About.vue"),
  // },
  // {
  //   path: "/menu",
  //   component: () => import("../views/menu/Index.vue"),
  // },
  // {
  //   path: "/order",
  //   component: () => import("../views/order/Index.vue"),
  // },
  // {
  //   path: "/search",
  //   component: () => import("../views/search/Index.vue"),
  // },
  // {
  //   path: "/worker",
  //   component: () => import("../views/worker/Index.vue"),
  // },
  // {
  //   path: "/inventory",
  //   component: () => import("../views/inventory/Index.vue"),
  // },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  // 1.如果访问的是登录页面（无需权限），直接放行
  if (to.path === "/login") return next();
  // 2.如果访问的是有登录权限的页面，先要获取token
  const tokenStr = window.sessionStorage.getItem("token");
  // 2.1如果token为空，强制跳转到登录页面；否则，直接放行
  if (!tokenStr) return next("/login");
  next();
});

export default router;
